package cn.fastoo.web.api.controller.user;


import cn.fastoo.alipay.AlipayConfig;
import cn.fastoo.alipay.AlipayHelper;
import cn.fastoo.common.dao.RechargeDao;
import cn.fastoo.common.dao.UserAccountDao;
import cn.fastoo.common.entity.Recharge;
import cn.fastoo.common.entity.UserAccount;
import cn.fastoo.common.entity.UserBase;
import cn.fastoo.common.entity.UserSmstemplate;
import cn.fastoo.common.sampleframe.SFBeanUtils;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.service.UserService;
import cn.fastoo.web.dto.RechargeDto;
import cn.fastoo.web.dto.UserAccountDto;
import cn.fastoo.web.dto.UserSignatureDto;
import cn.fastoo.web.dto.UserTemplateDto;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alipay.api.internal.util.AlipaySignature;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 签名
 * @author daniel.chan
 */
@Controller("rechargeController")
@RequestMapping("/v1/admin")
public class RechargeController {

	private final Logger logger = LoggerFactory.getLogger(RechargeController.class);

	@Autowired
	private RechargeDao rechargeDao;
	@Autowired
	private UserService userService;

	/**
	 * 充值记录
	 */
	@RequestMapping(value = "recharge")
	@ResponseBody
	public String recharge(UserBase userBase,Integer pageNo){
		pageNo = pageNo ==null ? 0 : pageNo;
		Page<Recharge> page = rechargeDao.findPage(new Search().addSearchFilter("EQ_userBaseId",userBase.getId())
				.addPageRequest(new PageRequest(pageNo,10,new Sort(Sort.Direction.DESC,"id"))));


		//包装recharge数据
		List<RechargeDto> rechargeDtos = new ArrayList<RechargeDto>();
		List<Recharge> list = page.getContent();
		for (Recharge recharge:list) {
			rechargeDtos.add(SFBeanUtils.injectByObject(new RechargeDto(),recharge));
		}

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("recharges",rechargeDtos);
		jsonObject.put("totalElements",page.getTotalElements());
		jsonObject.put("size",page.getSize());
		jsonObject.put("totalPages",page.getTotalPages());

		return new JsonResult(0,"ok",jsonObject).toJson();
	}

	/**
	 * alipay
	 */
	@RequestMapping(value = "recharge/alipay")
	@ResponseBody
	public String alipay(UserBase userBase,Double money) throws Exception{
		if (money == null) {
			return JsonResult.create(400,"非法访问");
		}
		if (money < 0.01f) {
			return JsonResult.create(401,"充值金额不能小于0.01元");
		}
		String subject = "在线充值" + money + "元";

		Recharge recharge = new Recharge();
		recharge.setUserBaseId(userBase.getId());
		recharge.setRechargeNo(""+new Date().getTime());
		recharge.setBalance(userBase.getBalance());
		recharge.setCreateTime(new Date());
		//totalprice充值金额
		recharge.setTotalPrice(money);
		recharge.setStatus(0);
		rechargeDao.insert(recharge);

		String orderNo = recharge.getRechargeNo();
		String textHtml = AlipayHelper.buildRequest(orderNo, subject, money.toString());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("textHtml",textHtml);
		return new JsonResult(0,"ok",jsonObject).toJson();
	}

	/**
	 * alipay
	 */
	@RequestMapping(value = "recharge/test")
	public void alipay(UserBase userBase, Double money, HttpServletResponse response) throws Exception{
		if (money == null) {
//			return JsonResult.create(400,"非法访问");
		}
		if (money < 0.01f) {
//			return JsonResult.create(401,"充值金额不能小于0.01元");
		}
		String subject = "在线充值" + money + "元";

		Recharge recharge = new Recharge();
		recharge.setUserBaseId(userBase.getId());
		recharge.setRechargeNo(""+new Date().getTime());
		recharge.setBalance(userBase.getBalance());
		recharge.setCreateTime(new Date());
		//totalprice充值金额
		recharge.setTotalPrice(money);
		recharge.setStatus(0);
		rechargeDao.insert(recharge);

		String orderNo = recharge.getRechargeNo();
		String textHtml = AlipayHelper.buildRequest(orderNo, subject, money.toString());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("textHtml",textHtml);
		AlipayHelper.responseWrite(response, textHtml);
//		return new JsonResult(0,"ok",jsonObject).toJson();
	}


}
